//
//  YmjLayout.m
//  AliBall
//
//  Created by yh on 16/8/14.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "YmjLayout.h"
#define SCREENWIDTH 320
#define INDEXPATNRAW [self.collectionView numberOfItemsInSection:j]
#define Height_first 0
#define Height_seccond 50
#define Height_third 100
#define Height_Fourth 150
#define LINESPACING 10
@interface YmjLayout ()
@property (nonatomic, strong) NSMutableArray *array_att;
@property (nonatomic, assign) CGFloat height_y;
@end
@implementation YmjLayout
- (instancetype)init {
    self = [super init];
    if (self) {
        _array_att = [NSMutableArray array];
        _height_y = Height_first + Height_seccond + Height_third + Height_seccond + Height_Fourth + Height_seccond + Height_Fourth * 2 + Height_seccond + Height_Fourth * 2 + Height_seccond + Height_Fourth + Height_seccond;
    }
    return self;
}
- (void)prepareLayout {
    [super prepareLayout];
    for (int j = 0; j < [self.collectionView numberOfSections]; j++) {
         if (j == 0) {
            for (int i = 0; i < [self.collectionView numberOfItemsInSection:j]; i++) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
                UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
                if (i == 0) {
                    att.frame = CGRectMake(0, Height_first, SCREENWIDTH, Height_seccond);
                    [_array_att addObject:att];
                } else {
                    att.frame = CGRectMake(LINESPACING + (145 + LINESPACING) * (i - 1) , Height_first + Height_seccond,  145, 100);
                    [_array_att addObject:att];
                }
                
            }
         } else if (j == 1) {
             for (int i = 0; i < [self.collectionView numberOfItemsInSection:j]; i++) {
                 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
                 UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
                 if (i == 0) {
                     att.frame = CGRectMake(0, Height_first + Height_seccond + Height_third, SCREENWIDTH, Height_seccond);
                     [_array_att addObject:att];
                 } else {
                     att.frame = CGRectMake(LINESPACING + (LINESPACING + 280 / 3) * (i - 1), Height_first + Height_seccond + Height_third + Height_seccond, 280 / 3, Height_Fourth);
                     [_array_att addObject:att];
                 }
             }
         } else if (j == 2) {
             NSMutableArray *minX_arr = [NSMutableArray arrayWithObjects:@(0),@(0),@(0), nil];
             NSInteger min_x = 0;
             for (int i = 0; i < [self.collectionView numberOfItemsInSection:j]; i++) {
                 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
                 UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
                 if (i == 0) {
                     att.frame = CGRectMake(0,Height_first + Height_seccond + Height_third + Height_seccond + Height_Fourth,SCREENWIDTH,Height_seccond);
                     [_array_att addObject:att];
                 } else {
                     CGFloat x = LINESPACING + (LINESPACING + 280 / 3) * min_x;
                     CGFloat y = (Height_first + Height_seccond + Height_third + Height_seccond + Height_Fourth + Height_seccond) + [minX_arr[min_x] floatValue];
                     CGFloat width = 280 / 3;
                     CGFloat height = Height_Fourth;
                     att.frame = CGRectMake(x,y,width,height);
                     [_array_att addObject:att];
                     
                     NSLog(@"啊啊啊啊啊%ld",min_x);
                     NSLog(@"哦哦哦哦哦%@",att);
                     if (min_x == 2) {
                         min_x = 0;
                         minX_arr = [NSMutableArray arrayWithObjects:@(height),@(height),@(height), nil];
                     } else {
                         min_x ++;
                         
                     }
                     
                 }
             }
         } else if (j == 3) {
             NSMutableArray *minX_arr = [NSMutableArray arrayWithObjects:@(0),@(0),@(0), nil];
             NSInteger min_x = 0;
             for (int i = 0; i < [self.collectionView numberOfItemsInSection:j]; i++) {
                 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
                 UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
                 if (i == 0) {
                     att.frame = CGRectMake(0,Height_first + Height_seccond + Height_third + Height_seccond + Height_Fourth + Height_seccond + Height_Fourth * 2,SCREENWIDTH,Height_seccond);
                     [_array_att addObject:att];
                 } else {
                     CGFloat x = LINESPACING + (LINESPACING + 280 / 3) * min_x;
                     CGFloat y = (Height_first + Height_seccond + Height_third + Height_seccond + Height_Fourth + Height_seccond + Height_Fourth * 2 + Height_seccond) + [minX_arr[min_x] floatValue];
                     CGFloat width = 280 / 3;
                     CGFloat height = Height_Fourth;
                     att.frame = CGRectMake(x,y,width,height);
                     [_array_att addObject:att];
                     
                     NSLog(@"啊啊啊啊啊%ld",min_x);
                     NSLog(@"哦哦哦哦哦%@",att);
                     if (min_x == 2) {
                         min_x = 0;
                         minX_arr = [NSMutableArray arrayWithObjects:@(height),@(height),@(height), nil];
                     } else {
                         min_x ++;
                         
                     }
                     
                 }
             }
         } else if (j == 4) {
             for (int i = 0; i < [self.collectionView numberOfItemsInSection:j]; i++) {
                 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
                 UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
                 if (i == 0) {
                     att.frame = CGRectMake(0, Height_first + Height_seccond + Height_third + Height_seccond + Height_Fourth + Height_seccond + Height_Fourth * 2 + Height_seccond + Height_Fourth * 2, SCREENWIDTH, Height_seccond);
                     [_array_att addObject:att];
                 } else {
                     att.frame = CGRectMake(LINESPACING + (LINESPACING + 280 / 3) * (i - 1), Height_first + Height_seccond + Height_third + Height_seccond + Height_Fourth + Height_seccond + Height_Fourth * 2 + Height_seccond + Height_Fourth * 2 + Height_seccond, 280 / 3, Height_Fourth);
                     [_array_att addObject:att];
                 }
             }
         } else {
             
             for (int i = 0; i < [self.collectionView numberOfItemsInSection:j]; i++) {
                 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
                 UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
                 if (i == 0) {
                     att.frame = CGRectMake(0, Height_first + Height_seccond + Height_third + Height_seccond + Height_Fourth + Height_seccond + Height_Fourth * 2 + Height_seccond + Height_Fourth * 2 + Height_seccond + Height_Fourth, SCREENWIDTH, Height_seccond);
                     [_array_att addObject:att];
                 } else {
                     att.frame = CGRectMake(10, _height_y, SCREENWIDTH, 80);
                     [_array_att addObject:att];
                     _height_y = _height_y + 80;
                 }
             }
         }
    }
}
- (CGSize)collectionViewContentSize {
    return CGSizeMake(0, _height_y);
}
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect; {
    return _array_att;
}
@end
